package com.itheima.health.filter;

import com.alibaba.fastjson.JSON;
import com.itheima.health.common.BaseContext;
import com.itheima.health.common.R;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.AntPathMatcher;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;

/**
 * 登录检测过滤器
 */
@Slf4j
@WebFilter(urlPatterns = "/*") //过滤器 取名
@Configuration
public class LoginCheckFilter  implements Filter {
    //路径匹配器
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;

        System.out.println("进入过滤器");
        //静态资源放行
        String[] letGo = {
                //静态资源
                "/backend/img/**",
                //"/backend/api/**",
                "/backend/js/**",
                "/backend/pages/**",
                "/backend/css/**",
                "/backend/plugins/**",
                "/favicon.ico",
                "/backend",
                "/backend/",
                "/backend/index.html",

                //通用工具
                "/common/**",
                //登录用
                "/site/**",
        };
        boolean checkResult = check(letGo,request.getRequestURI());
        if(true){
            //允许通过
            filterChain.doFilter(request,response);
            return;
        }

        log.info("过滤器:{}",request.getRequestURI());
        //判断登录状态
        Object employee = request.getSession().getAttribute("employee");
        System.out.println(employee);
        if(Objects.nonNull(employee)){
            Long id = (Long)employee;
            log.info("已登录的ID:"+ id);
            BaseContext.setThreadLocal(id);
            //已登录
            filterChain.doFilter(request,response);
            return;
        }
        boolean checkResult = check(letGo,request.getRequestURI());
        if(true){//checkResult
            //允许通过
            filterChain.doFilter(request,response);
            return;
        }

//        log.info("过滤器:{}",request.getRequestURI());
//        //判断登录状态
//        Object employee = request.getSession().getAttribute("employee");
//        System.out.println(employee);
//        if(Objects.nonNull(employee)){
//            Long id = (Long)employee;
//            log.info("已登录的ID:"+ id);
//            BaseContext.setThreadLocal(id);
//            //已登录
//            filterChain.doFilter(request,response);
//            return;
//        }
//        //客户端登录检测
//        Object client = request.getSession().getAttribute("client");
//        System.out.println(client);
//        if(Objects.nonNull(client)){
//            Long id = (Long)client;
//            log.info("客户端的ID:"+ id);
//            BaseContext.setThreadLocal(id);
//            //已登录
//            filterChain.doFilter(request,response);
//            return;
//        }
//
//        log.info("非法访问");
//        request.setCharacterEncoding("UTF-8");
//        //判断是不是ajax
//        boolean isAjaxRequest = false;
//        if(!StringUtils.isBlank(request.getHeader("x-requested-with")) && request.getHeader("x-requested-with").equals("XMLHttpRequest")){
//            isAjaxRequest = true;
//        }
//        if(isAjaxRequest) {
//            response.setContentType("application/json;charset=utf-8");
//            //未登录
//            response.getWriter().write(JSON.toJSONString(R.error("未登录")));
//            return;
//        }
//        response.sendRedirect("/front/page/login.html");


    }

    /**
     * 匹配路由
     * @param urls
     * @param uri
     * @return
     */
    public boolean check(String[] urls , String uri){
        for (String url : urls) {
            boolean match = PATH_MATCHER.match(url, uri);
            if(match) {
                return true;
            }
        }
        return false;
    }
}
